package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/23 15:57
 * @Version V1.0
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDao menuDao;
    @Override
    public List<Menu> getFindMenu() {
        //获取所有的父类标签
       List<Menu> parents= menuDao.getParent();
        for (Menu parent : parents) {
            //得到父类标签id
            Integer id = parent.getId();
            //通过父类id找到所有的子类标签
          List<Menu> subclass=menuDao.getSubclass(id);
          parent.setChildren(subclass);
        }
        return parents;
    }


    @Override
    public void add(Menu menu) {


        menuDao.add(menu);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        // 1：完成对分页初始化工作
        PageHelper.startPage(currentPage,pageSize);
        // 2：查询
        Page<Menu> page = menuDao.findPage(queryString);
        // 组织PageResult
        return new PageResult(page.getTotal(),page.getResult());


    }

    @Override
    public void deleteById(Integer id) {

        // 使用检查项id，查询检查项
        Integer count = menuDao.findCheckGroupAndCheckItemCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException("当前检查项和检查组之间存在关联关系，不能删除");
        }
        menuDao.deleteById(id);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {

        menuDao.editMenuBy(menu);
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Integer> findMenuIds(Integer id) {
        return menuDao.findRoleAndMenu(id);
    }

}
